package com.itheima.controller;

import com.itheima.common.PageBean;
import com.itheima.common.Result;
import com.itheima.pojo.Emp;
import com.itheima.service.EmpService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.List;

/**
 * 员工管理Controller
 */
@Slf4j
@RestController
@RequestMapping("/emps")
public class EmpController {
    @Autowired
    private EmpService empService;

    /**
     * 查询一串员工信息
     * @param name
     * @param gender
     * @param begin
     * @param end
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping
    public Result<PageBean<Emp>> selectPage(
            String name ,
            Integer gender,
            @DateTimeFormat(pattern = "yyyy-MM-dd")LocalDate begin,
            @DateTimeFormat(pattern = "yyyy-MM-dd")LocalDate end,
            @RequestParam(defaultValue = "1")Integer page,
            @RequestParam(defaultValue = "10")Integer pageSize
            ){
    PageBean<Emp> empPageBean = empService.selectPage(
            name,
            gender,begin,end,page,pageSize
    );

    return Result.success(empPageBean);
    }

    /**
     * 查询一个员工
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result<Emp> selectOne(@PathVariable Integer id){
        Emp emp = empService.selectOne(id);

        return Result.success(emp);
    }

    /**
     * 删除一个员工
     * @param ids
     * @return
     */
    @DeleteMapping("/{ids}")
    public Result<?> deleteById(@PathVariable List<Integer> ids){
        empService.deletelist(ids);

        return Result.success();
    }

    /**
     * 添加一个员工
     * @param emp
     * @return
     */
    @PostMapping
    public Result insertOne(@RequestBody Emp emp){
        empService.insertOne(emp);

        return Result.success();
    }

    /**
     * 修改一个员工
     * @param emp
     * @return
     */
    @PutMapping
    public Result<?> updateOne(@RequestBody Emp emp){
        empService.updateOne(emp);

        return Result.success();
    }




}
